home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-18 | 7.3 KB | 273 lines |
- /*
- * @(#)NSResponse.java 1.9 97/05/22
- *
- * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
- *
- * This software is the confidential and proprietary information of Sun
- * Microsystems, Inc. ("Confidential Information"). You shall not
- * disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Sun.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
- * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- *
- * CopyrightVersion 1.0
- */
-
- package sun.servlet.netscape;
-
- import java.io.IOException;
- import java.io.PrintStream;
- import java.util.Date;
- import java.util.Observer;
- import java.util.Observable;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletResponse;
- import sun.servlet.http.HttpOutputStream;
- import netscape.server.applet.ServerApplet;
- import netscape.server.applet.Server;
-
- /*
- * This class represents a servlet response for servlets running in Netscape's
- * Enterprise or FastTrack servers. The response object is passed to the
- * servlet by a special server applet.
- *
- * @version 1.9, 05/22/97
- * @author David Connelly
- */
- public
- class NSResponse implements HttpServletResponse, Observer {
- /*
- * The servlet output stream for this response.
- */
- private HttpOutputStream out = new HttpOutputStream();
-
- /*
- * The servlet runner for this response.
- */
- private NSRunner sr;
-
- /*
- * Initializes this response.
- */
- void init(NSRunner sr) throws IOException {
- this.sr = sr;
- out.init(sr.getOutputStream());
- out.next();
- out.setObserver(this);
- }
-
- /**
- * Returns true if the specified header field is contained in this
- * response otherwise returns false.
- * @param name the header field name
- */
- public boolean containsHeader(String name) {
- return sr.getResponseProperty(name.toLowerCase()) != null;
- }
-
- /*
- * Resets the response.
- */
- void reset() {
- this.sr = null;
- out.resets();
- }
-
- /*
- * Finishes the response.
- */
- void finish() throws IOException {
- out.finish();
- }
-
- /**
- * Sets the content length of the response.
- * @param len the content length
- */
- public void setContentLength(int len) {
- sr.setResponseProperty("content-length", Integer.toString(len));
- out.setContentLength(len);
- }
-
- /**
- * Sets the content type for the response.
- * @param type the content type
- */
- public void setContentType(String type) {
- sr.setResponseProperty("content-type", type);
- }
-
- /**
- * Returns the servlet output stream for writing response data.
- * @exception IOException if an I/O error has occurred
- */
- public ServletOutputStream getOutputStream() {
- return out;
- }
-
- /**
- * Sets the status code and reason phrase for the response.
- * @param code the status code
- * @param reason the reason phrase
- */
- public void setStatus(int code, String reason) {
- sr.setStatus(code, reason);
- }
-
- /**
- * Sets the status code of the response with a default reason phrase.
- * @param code the status code
- */
- public void setStatus(int code) {
- sr.setStatus(code);
- }
-
- /**
- * Sets a response header.
- * @param name the header name
- * @param value the header value
- */
- public void setHeader(String name, String value) {
- sr.setResponseProperty(name.toLowerCase(), value);
- }
-
- /**
- * Sets an integer response header.
- * @param name the header name
- * @param value the header integer value
- */
- public void setIntHeader(String name, int value) {
- sr.setResponseProperty(name.toLowerCase(), Integer.toString(value));
- }
-
- /**
- * Sets a date response header.
- * @param name the header name
- * @param value the header date value
- */
- public void setDateHeader(String name, long value) {
- sr.setResponseProperty(name.toLowerCase(), new Date(value).toString());
- }
-
- /**
- * Sends an error response using the specified status code and detail
- * message.
- * @param status the status code
- */
- public void sendError(int status) throws IOException {
- sendError(status, status + " " + reason(status));
- }
-
- /**
- * Sends an error response using the specified status code and detail
- * message.
- * @param status the status code
- * @param detail the error message
- */
- public void sendError(int status, String detail) throws IOException {
- if (out.getTotal() > 0) {
- return;
- }
- if (sr.returnErrorResponse("text/html", status, reason(status))) {
- PrintStream out = sr.getOutputStream();
- out.println("<html>");
- out.println("<title>" + detail + "</title>");
- out.println(detail);
- out.println("</html>");
- out.flush();
- }
- }
-
- /**
- * Sends a redirect response to the client using the specified redirect
- * location URL.
- * @param location the redirect location
- */
- public void sendRedirect(String location) throws IOException {
- if (out.getTotal() > 0) {
- return;
- }
- sr.setResponseProperty("location", location);
- if (sr.returnErrorResponse("text/html",
- sr.REDIRECT, reason(sr.REDIRECT))) {
- PrintStream out = sr.getOutputStream();
- out.println("<html><title>Document moved</title>/</html>");
- out.println("<body><h1>Document moved</h1>");
- out.print("This document has moved <a href=\"");
- out.print(location);
- out.println("\">here</a>.<p></body>");
- out.flush();
- }
- }
-
- /*
- * Returns reason phrase message for specified status code.
- */
- private static String reason(int status) {
- switch (status) {
- case SC_OK:
- return "OK";
- case SC_CREATED:
- return "Created";
- case SC_ACCEPTED:
- return "Accepted";
- case SC_NO_CONTENT:
- return "No Content";
- case SC_MOVED_PERMANENTLY:
- return "Moved Permanently";
- case SC_MOVED_TEMPORARILY:
- return "Moved Temporarily";
- case SC_NOT_MODIFIED:
- return "Not Modified";
- case SC_BAD_REQUEST:
- return "Bad Request";
- case SC_UNAUTHORIZED:
- return "Unauthorized";
- case SC_FORBIDDEN:
- return "Forbidden";
- case SC_NOT_FOUND:
- return "Not Found";
- case SC_INTERNAL_SERVER_ERROR:
- return "Internal Server Error";
- case SC_NOT_IMPLEMENTED:
- return "Not Implemented";
- case SC_BAD_GATEWAY:
- return "Bad Gateway";
- case SC_SERVICE_UNAVAILABLE:
- return "Service Unavailable";
- default:
- return "Unknown";
- }
- }
-
- /**
- * Notifies response object when output stream has been first written.
- */
- public void update(Observable obs, Object obj) {
- HttpOutputStream out;
-
- try {
- out = (HttpOutputStream)obj;
- } catch (ClassCastException e) {
- throw new InternalError("update called with invalid argument type");
- }
- try {
- if (!containsHeader("status")) { //yes, it isn't a header...
- setStatus(SC_OK,reason(SC_OK));
- }
- if (!containsHeader("content-type")) {
- setContentType("text/html");
- }
- sr.startResponse();
- } catch (IOException e) {
- out.setIOException(e);
- }
- }
- }
-